<cfparam name="view" default="" />
<cfparam name="action" default="" />
<cfparam name="alert" default="" />
<cfparam name="email" default="" />
<cfparam name="j_username" default="" />
<cflogout />
<cfswitch expression="#action#">
	<cfcase value="emailPassword">
		<cfscript>
			base = new assets.calgrants.com.Base();
			emailResult = base.emailPassword(Email = Email);
		</cfscript>
		<cfif emailResult>
			<cfset alert = "Your password has been sent to #email#" />
			<cfset view = "login" />
		<cfelse>
			<cfset alert = "The e-mail address #email# was not found.  Verify your e-mail address and try again." />
			<cfset view = "password" />
		</cfif>
	</cfcase>
	<cfcase value="checkEmail">
		<cfscript>
			userGateway = new assets.calgrants.com.gateway.UserGateway();
			usersWithEmail = userGateway.query(Email = Email, Deleted = 0, Archived = 0);
		</cfscript>
		<cfif usersWithEmail.RecordCount gt 1>
			<cfset alert = "An account already exists for #email#, however there is a problem with the account.  Please contact #application.personality.name# at #application.personality.email# to resolve this issue." />
			<cfset view = "login" />
		<cfelseif usersWithEmail.RecordCount eq 1>
			<cfset view = "userExists" />
		<cfelseif usersWithEmail.RecordCount eq 0>
			<cfset view = "user" />
		</cfif>
	</cfcase>
	<cfcase value="packUser">
		<cfset formClone = Duplicate(form) />
		<cfset StructDelete(formClone, "action") />
		<cfset StructDelete(formClone, "fieldnames") />
		<cfwddx action="cfml2wddx" input="#formClone#" output="userWDDX" />
		<cfset view = "organizations" />
	</cfcase>
	<cfcase value="checkOrganization">
		<cfif Organization_ID eq 0>
			<cfset view = "organization" />
		<cfelseif Organization_ID gt 0>
			<cfscript>
				base = new assets.calgrants.com.Base();
				userGateway = new assets.calgrants.com.gateway.UserGateway();
				organizers = userGateway.query(Organization_ID = Organization_ID, Approved = 1, Role = "Organizer");
			</cfscript>
			<cfwddx action="wddx2cfml" input="#userWDDX#" output="user" />
			<cfset user.Organization_ID = Organization_ID />
			<cfif organizers.RecordCount eq 0>
				<cfset user.Approved = 1 />
				<cfset user.Role = "Organizer" />
				<cfset view = "createdNew" />
			<cfelseif organizers.RecordCount gt 0>
				<cfset user.Approved = 1 />
				<cfset user.Role = "Organizer" />
				<cfset view = "createdOnExisting" />
			</cfif>
			<cfscript>
				user = userGateway.save(argumentCollection = user);
			</cfscript>
			<cfif NOT IsInstanceOf(user, "assets.calgrants.com.bo.User")>
				<cfset alert = "There was a problem creating your account.  Please try again.">
				<cfset view = "error">
			<cfelse>
				<cfset emailResult = base.emailNewUserNotification(User_ID = user.getUser_ID()) />
			</cfif>
		</cfif>	
	</cfcase>
	<cfcase value="createAccount">
		<cfwddx action="wddx2cfml" input="#form.userWDDX#" output="userArgs" />
		<cfif form.Organization_ID eq 0>
			<cfscript>
				base = new assets.calgrants.com.Base();
				organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
				organization = organizationGateway.save(argumentCollection = form);
			</cfscript>
			<!--- need to mail notification to CCFC that new organization is create and pending approval --->
			<cfif NOT IsInstanceOf(organization, "assets.calgrants.com.bo.Organization")>
				<cfset alert = "There was a problem creating your account.  Please try again.">
				<cfset view = "error">
			<cfelse>
				<cfset userArgs.Organization_ID = organization.getOrganization_ID()>
				<cfset userArgs.Approved = 1 />
				<cfset userArgs.Role = "Organizer" />
				<cfscript>
					userGateway = new assets.calgrants.com.gateway.UserGateway();
					user = userGateway.save(argumentCollection = userArgs);
				</cfscript>
				<cfif NOT IsInstanceOf(user, "assets.calgrants.com.bo.User")>
					<cfset alert = "There was a problem creating your account.  Please try again.">
					<cfset view = "error">
				<cfelse>
					<cfset view = "createdNew" />
					<cfset emailResult = base.emailNewUserNotification(User_ID = user.getUser_ID()) />
				</cfif>
			</cfif>
		<cfelse>
			<cfset userArgs.Approved = 1 />
			<cfset userArgs.Role = "Organizer" />
			<cfscript>
				base = new assets.calgrants.com.Base();
				userGateway = new assets.calgrants.com.gateway.UserGateway();
				user = userGateway.save(argumentCollection = userArgs);
			</cfscript>
			<cfif NOT IsInstanceOf(user, "assets.calgrants.com.bo.User")>
				<cfset alert = "There was a problem creating your account.  Please try again.">
				<cfset view = "error">
			<cfelse>
				<cfset view = "createdOnExisting" />
				<cfset emailResult = base.emailNewUserNotification(User_ID = user.getUser_ID()) />
			</cfif>
		</cfif>
	</cfcase>
</cfswitch>

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Cash for College Workshop Management System</title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="description" content="Cash for College Workshop Management System">
	<meta name="author" content="Syntric">
	<cfoutput>
		<link href="#request.pathToBootstrapCSS#" rel="stylesheet" />
		<link href="#request.pathToKendoCommonCSS#" rel="stylesheet" />
		<link href="#request.pathToKendoDefaultCSS#" rel="stylesheet" />
		<link href="#request.pathToFontAwesomeCSS#" rel="stylesheet" />
		<link href="#request.pathToUI#/common.css" rel="stylesheet" />
		<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet" type="text/css" />
		<script src="#request.pathToJQuery#" type="text/javascript"></script>
		<script src="#request.pathToBootstrap#" type="text/javascript"></script>
		<script src="#request.pathToKendo#" type="text/javascript"></script>
		<script src="#request.pathToUI#/common.js" type="text/javascript"></script>
		<script type="text/javascript">
			<cfif Len(alert) gt 0>
				alert("#alert#");
			</cfif>
			<cfif ArrayLen(request.alerts) gt 0>
				<cfloop index="i" from="1" to="#ArrayLen(request.alerts)#">
					alert("#request.alerts[i]#");
				</cfloop>
			</cfif>
			/*
			var loginReg = function() {
				var email = $("##j_username").val();
				if (email == "despinacosto@gmail.com" || email == "despina@ccfc.org" || email == "ken@syntric.com" || email == "swilliams@msjc.edu") {
					var f = $("##loginForm");
					f.submit();
				} else {
					alert("Registration for the 2014-2015 Cash for College Program is not open yet.\n\nOrganizers from prior program year will be notified by email when the registration opens.");
				}
				return true;				
			}
			*/
		</script>
	</cfoutput>
</head>
<body style="background-color: white;">
<cfoutput>
	<div class="container-fluid">
		<div class="row-fluid" style="height: 20px;">
			<div class="span12"></div>
		</div>
		<div class="row-fluid">
			<div class="span3"></div>
			<div class="span6">
				<img src="common/images/Cash_for_College_logo.gif" width="204" height="54" style="margin-bottom: 8px;">
				<p><strong>Workshop Management System for Organizers &amp; Site Contacts</strong></p>
				<div class="well well-large well-framed" style="width: 400px; margin-bottom: 40px;">
					<cfswitch expression="#view#">
						<cfcase value="password">
							<cfform id="passwordForm" action="?action=emailPassword" method="post">
								<div class="clearfix">
									<label for="email">Enter your e-mail address</label>
									<div class="input-prepend">
										<span class="add-on">
											<i class="icon-envelope"></i>
										</span>
										<cfinput type="text" name="email" value="#email#"  validate="email" required="yes" message="E-mail is required and must be a valid e-mail address" style="width: 310px;" />
									</div>
								</div>
								<div class="btn-toolbar clearfix">
									<button type="submit" name="sendPasswordButton" class="btn btn-success">Send Password</button>
									<button type="button" name="cancelButton" class="btn btn-success" onClick="document.location.href='?view=login'">Cancel</button>
								</div>
							</cfform>
						</cfcase>
						<cfcase value="email">
							<cfform id="emailForm" action="#script_name#" method="post">
								<cfinput type="hidden" name="action" value="checkEmail">
								<div class="clearfix">
									<label for="name">Enter your e-mail address</label>
									<div class="input-prepend">
										<span class="add-on">
											<i class="icon-envelope"></i>
										</span>
										<cfinput type="text" name="email" value="#email#" required="yes" message="E-mail is required and must be a valid e-mail address" style="width: 310px;" />
									</div>
								</div>
								<div class="btn-toolbar clearfix">
									<button type="submit" class="btn btn-success">Next</button>
									<button type="button" name="cancelButton" class="btn btn-success" onClick="document.location.href='?view=login'">Cancel</button>
								</div>
							</cfform>
						</cfcase>
						<cfcase value="userExists">
							<div class="clearfix">
								An account already exists for #email#.
							</div>
							<div class="btn-toolbar clearfix">
								<button type="button" name="sendPasswordButton" class="btn btn-success" onClick="document.location.href='?action=emailPassword&Email=#email#'">Send Password</button>
								<button type="button" name="cancelButton" class="btn btn-success" onClick="document.location.href='?view=login'">Cancel</button>
							</div>
						</cfcase>
						<cfcase value="user">
							<cfform id="userForm" action="#script_name#" method="post">
								<cfinput type="hidden" name="action" value="packUser" />
								<cfinput type="hidden" name="user_id" value="0" />
								<cfinput type="hidden" name="email" value="#email#" />
								<div class="clearfix">
									<label for="name">First Name</label>
									<cfinput type="text" name="first_name" value="" required="yes" message="First Name is required" class="span12" />
								</div>
								<div class="clearfix">
									<label for="name">Last Name</label>
									<cfinput type="text" name="last_name" value="" required="yes" message="Last Name is required" class="span12" />
								</div>
								<div class="clearfix">
									<label for="title">Title</label>
									<cfinput type="text" name="title" value="" class="span12" />
								</div>
								<div class="text-control clearfix">
									<label for="email">E-mail</label>
									#email#
								</div>
								<div class="clearfix">
									<label for="password">Password</label>
									<cfinput type="password" name="password" value="" required="yes" message="Password is required" class="span12" />
								</div>
								<div class="row-fluid clearfix">
									<div class="span9">
										<label for="phone">Phone</label>
										<cfinput type="text" name="phone" value="" required="yes" validate="telephone" message="Phone is required and must be in a valid format" class="span12" />
									</div>
									<div class="span3">
										<label for="phone">Extension</label>
										<cfinput type="text" name="Extension" value="" required="no" maxlength="10" class="span12" />
									</div>
								</div>
								<div class="btn-toolbar clearfix">
									<button type="submit" class="btn btn-success">Next</button>
									<button type="button" name="cancelButton" class="btn btn-success" onClick="document.location.href='?view=login'">Cancel</button>
								</div>
							</cfform>
						</cfcase>
						<cfcase value="organizations">
							<cfscript>
								organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
								organizations = organizationGateway.query(Approved = 1, Archived = 0);
							</cfscript>
							<cfform id="organizationsForm" action="#script_name#" method="post">
								<cfinput type="hidden" name="action" value="checkOrganization">
								<cfinput type="hidden" name="userWDDX" value="#userWDDX#" />
								<div class="clearfix">
									<label for="name">Select your organization</label>
									<p>If your organization is not listed, select &quot;My organization is not listed&quot; at the bottom of the list.</p>
									<cfselect name="organization_id" size="20" required="yes" message="Organization is required.\n\nIf your organization is not listed select \'Not listed.\'" style="width: 100%; font-size: 0.8em;">
										<cfloop query="organizations">
											<option value="#organization_id#">#organization#, #city#</option>
										</cfloop>
										<option value="0">My organization is not listed</option>
									</cfselect>
								</div>
								<div class="btn-toolbar clearfix">
									<button type="submit" class="btn btn-success">Next</button>
									<button type="button" name="cancelButton" class="btn btn-success" onClick="document.location.href='?view=login'">Cancel</button>
								</div>
							</cfform>
						</cfcase>
						<cfcase value="organization">
							<cfscript>
								countyGateway = new assets.calgrants.com.gateway.CountyGateway();
								counties = countyGateway.query();
							</cfscript>
							<cfform id="organizationForm" action="#script_name#" method="post">
								<cfinput type="hidden" name="action" value="createAccount">
								<cfinput type="hidden" name="userWDDX" value="#userWDDX#">
								<cfinput type="hidden" name="organization_id" value="#organization_id#" />
								<div class="clearfix">
									<label for="name">Organization</label>
									<cfinput type="text" name="organization" value="" required="yes" message="Organization is required" class="span12" />
								</div>
								<div class="clearfix">
									<label for="name">Address</label>
									<cfinput type="text" name="address" value="" required="yes" message="Address is required" class="span12" />
								</div>
								<div class="clearfix">
									<label for="name">City</label>
									<cfinput type="text" name="city" value="" required="yes" message="City is required" class="span12" />
								</div>
								<div class="clearfix">
									<label for="name">Zip Code</label>
									<cfinput type="text" name="zip_code" value="" required="yes" message="Zip Code is required" class="span3" />
								</div>
								<div class="clearfix">
									<label for="name">County</label>
									<cfselect name="county_id" required="yes" message="County is required" class="span12">
										<option value="">Select...</option>
										<cfloop query="counties">
											<option value="#county_id#">#county#</option>
										</cfloop>
									</cfselect>
								</div>
								<div class="btn-toolbar clearfix">
									<button type="submit" class="btn btn-success">Create Account</button>
									<button type="button" name="cancelButton" class="btn btn-success" onClick="document.location.href='?view=login'">Cancel</button>
								</div>
							</cfform>
						</cfcase>
						<cfcase value="createdNew">
							<p>Your account has been created.  You can now login.</p>
							<p><a href="?view=login" class="btn btn-success">Go to Login</a></p>
						</cfcase>
						<cfcase value="createdOnExisting">
							<cfscript>
								organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
								qOrganization = organizationGateway.query(Organization_ID = Organization_ID);
								userGateway = new assets.calgrants.com.gateway.UserGateway();
								qRegionalAdmins = userGateway.query(Approved = 1, Archived = 0, Role = "Regional Admin", Region_IDs = qOrganization.Region_ID);
							</cfscript>
							<p>Your account has been created.  However it must be approved before you can login.</p>
							<p>Any of the following people can approve your account:</p>
							<ul>
								<cfloop query="organizers">
									<li>#first_name# #last_name# - #email# - #phone#<cfif Len(Trim(extension)) gt 0> x #extension#</cfif></li>
								</cfloop>
							</ul>
							<p>Each person listed above has been notified that your account requires approval before you can login.</p>
							<p>If none of the people listed above are available to approve your account you can contact:</p>
							<ul>
								<cfloop query="qRegionalAdmins">
									<li>#first_name# #last_name# - #email# - #phone#<cfif Len(Trim(extension)) gt 0> x #extension#</cfif> (Regional Administrator)</li>
								</cfloop>
								<cfif qRegionalAdmins.RecordCount eq 0>
									<li>#request.personality.name# - #request.personality.email# - #request.personality.phone# (Statewide Coordinator)</li>
									<!--- <li>#request.developer.name# - #request.developer.email# - #request.developer.phone# (Developer)</li> --->
								</cfif>
							</ul>
							<p><a href="?view=login" class="btn btn-success">Go to Login</a></p>
						</cfcase>
						<cfcase value="error">
							<p>There was a problem with your submission.</p>
							<p>Please start over.</p>
							<p><a href="?view=login" class="btn btn-success">Go to Login</a></p>
						</cfcase>
						<!--- login --->
						<cfdefaultcase>
							<cfform id="loginForm" action="#script_name#" method="post">
								<div class="clearfix">
									<label for="name">E-mail</label>
									<div class="input-prepend">
										<span class="add-on">
											<i class="icon-envelope"></i>
										</span>
										<cfinput type="text" name="j_username" value="#j_username#" required="yes" validate="email" message="E-mail is required and must be a valid e-mail address" style="width: 310px;" />
									</div>
								</div>
								<div class="clearfix">
									<label for="password">Password</label>
									<div class="input-prepend">
										<span class="add-on">
											<i class="icon-lock"></i>
										</span>
										<cfinput type="password" name="j_password" required="yes" message="Password is required" style="width: 310px;" />
									</div>
								</div>
								<div class="btn-toolbar clearfix">
									<button type="submit" name="loginButton" class="btn btn-success">Login</button>
								</div>
							</cfform>
							<div style="margin-top: 10px;">
								<a href="?view=email">Create an account</a><br>
								<a href="?view=password">Forgot your password?</a>
							</div>
						</cfdefaultcase>
					</cfswitch>
				</div>
			</div>
			<div class="span3"></div>
		</div>
		<div class="row-fluid body-row">
			<div class="span3"></div>
			<div class="span6">
				<div style="margin-top: 20px;">
					<small>
						<p><strong>Cash for College</strong> is California's public-private partnership FAFSA completion program administered by the California Student Aid Commission and its regional and local partners.</p>
						<p><strong>Our mission</strong> is to assist high school seniors and recent graduates who are low income or first generation students in accessing as much financial aid as possible. California Cash for College puts California Dreams into Action by helping students complete the Free Application for Federal Student Aid (FAFSA) or California Dream Act Application. Students who apply for financial aid take action to enroll into education beyond high school.</p>
					</small>
				</div>
			</div>
			<div class="span3"></div>
		</div>
	</div>
</cfoutput>
</body>
</html>